package git.soulbgm.config;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ElasticJob配置类
 *
 * @author SoulBGM
 * @date 2020-09-14
 */
@Configuration
@ConfigurationProperties(prefix = "elastic.job.zk", ignoreUnknownFields = false)
@ConditionalOnExpression("'${elastic.job.zk.server-lists}'.length() > 0")
public class ElasticJobConfig {

    private String serverLists;

    private String namespace;

    @Bean
    public ZookeeperRegistryCenter initZookeeperRegistryCenter() {
        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(serverLists, namespace);
        zkConfig.setSessionTimeoutMilliseconds(100);
        ZookeeperRegistryCenter regCenter = new ZookeeperRegistryCenter(zkConfig);
        regCenter.init();
        return regCenter;
    }

    public String getServerLists() {
        return serverLists;
    }

    public void setServerLists(String serverLists) {
        this.serverLists = serverLists;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }
}
